<template>
  <div class="box">
    <div class="head-fixed">
      <div class="head">
        <div class="head-div">
          <img
            src="../../assets/zjt2.png"
            alt=""
            class="back"
            @click="back()"
          />
        </div>
        <div class="head-search">个人信息</div>
        <div @click="saveIndividual">保存</div>
      </div>
    </div>
    <div class="content">
      <div class="inpBox">
        <span>头像</span>
        <input
          v-show="false"
          type="file"
          accept="image/*"
          @change="triggerFile($event)"
          ref="input"
        />
        <div>
          <img
            src="../../assets/avatar.png"
            v-if="imgUrl === ''"
            alt=""
            @click="openImg"
          />
          <img :src="imgUrl" v-if="imgUrl !== ''" alt="" @click="openImg" />
        </div>
      </div>
      <div class="inpBox">
        <span>姓名</span>
        <div>
          <input
            type="text"
            v-model="individualData.userResume.userName"
            placeholder="请输入姓名"
          />
        </div>
      </div>
      <div class="inpBox">
        <span>性别</span>
        <div>
          <input
            type="radio"
            name="sex"
            v-model="individualData.userResume.sex"
            value="0"
          /><span>男</span>
          <input
            type="radio"
            name="sex"
            v-model="individualData.userResume.sex"
            value="1"
          /><span>女</span>
        </div>
      </div>
      <div class="inpBox">
        <span>出生日期</span>
        <div>
          <input
            type="date"
            v-model="individualData.userResume.birthdate"
            placeholder="请输入出生日期"
          />
        </div>
      </div>
      <div class="inpBox">
        <span>电话</span>
        <div>
          <input
            type="text"
            v-model="individualData.userResume.tel"
            placeholder="请输入电话号码"
          />
        </div>
      </div>
      <div class="inpBox">
        <span>微信</span>
        <div>
          <input
            type="text"
            v-model="individualData.userResume.wechat"
            placeholder="请输入微信"
          />
        </div>
      </div>
      <div class="inpBox">
        <span>邮箱</span>
        <div>
          <input
            type="text"
            v-model="individualData.userResume.email"
            placeholder="请输入邮箱"
          />
        </div>
      </div>
      <div class="inpBoxs">
        <div class="inpBox">
          <span>个人简介</span>
          <div style="color: #757575">选填</div>
        </div>
        <textarea
          name=""
          id=""
          placeholder="请描述你的个人经历..."
          v-model="individualData.userIndividual"
        ></textarea>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  inject: ["reload"],
  name: "usersInformation",
  components: {},
  created() {
    // created 在模板渲染成html前调用，即通常初始化某些属性值，然后再渲染成视图
    this.getInformationData();
  },
  mounted() {
    // mounted在模板渲染成html后调用，通常是初始化页面完成后，再对html的dom节点进行一些需要的操作
  },
  // data() 为变量赋值等
  data() {
    return {
      // status为用户身份，0是求职者，1是招聘者
      status: localStorage.status,
      userId: localStorage.getItem("userId"),
      imgUrl: "",
      check: 0,
      userName: "",
      birthday: "",
      phone: "",
      email: "",
      text: "",
      individualData: { userResume: { userName: "" } },
      userPortrait: "",
      file: ""
    };
  },
  // methods 编写js函数
  methods: {
    //保存个人信息数据
    saveIndividual() {
      let formData = new FormData();
      formData.append("file", this.file);
      formData.append("id", this.userId);
      formData.append("user_individual", this.individualData.userIndividual);
      formData.append("user_name", this.individualData.userResume.userName);
      formData.append("sex", this.individualData.userResume.sex);
      formData.append("birthdate", this.individualData.userResume.birthdate);
      formData.append("tel", this.individualData.userResume.tel);
      formData.append("wechat", this.individualData.userResume.wechat);
      formData.append("email", this.individualData.userResume.email);
      this.$http({
        method: "post",
        url: this.apiURL + "/user/saveIndividual",
        data: formData
      }).then(({ data }) => {
        console.log(data);
        if (data.code === 0) {
          this.$router.push({
            name: "usersResume"
          });
        }
      });
    },
    //得到信息数据
    getInformationData() {
      this.$http({
        method: "get",
        url: this.apiURL + "/user/Individual",
        params: {
          userId: this.userId
        }
      }).then(({ data }) => {
        this.individualData = data.Individual;
        this.imgUrl = this.apiURL + "/asjy/" + this.individualData.userPortrait;
        this.file = this.apiURL + "/asjy/" + this.individualData.userPortrait;
        // 发送请求获取文件内容
        this.getFileObject(this.file);
      });
    },
    // 获取文件对象的方法
    getFileObject(filePath) {
      // 发送请求获取文件内容
      this.$http({
        method: "get",
        url: filePath,
        responseType: "blob" // 设置响应类型为 Blob
      }).then(({ data }) => {
        // 将 Blob 对象传递给处理方法
        this.handleFileBlob(data);
      });
    },
    // 处理文件的方法
    handleFileBlob(blob) {
      let reader = new FileReader();
      reader.readAsDataURL(blob);
      reader.onload = e => {
        // 读取文件内容并将其转换为 base64 编码
        let url = e.target.result.substring(e.target.result.indexOf(",") + 1);
        // 更新用户头像的 URL 和数据
        this.imgUrl = "data:image/png;base64," + url;
        this.file = blob; // 将 Blob 对象存储到 this.file 中
      };
    },
    //change事件触发文件上传
    triggerFile: function(event) {
      let file = event.target.files[0];
      this.handleFileBlob(file);
    },
    openImg() {
      this.$refs.input.click();
    },
    //返回
    back() {
      this.$router.go(-1); //返回上一层
    }
  }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
html,
body,
.tac,
.box,
.tab-left,
.tab-left .el-menu {
  height: 100%;
}

.box {
  width: 100%;
  background: #fff;
}

.head-fixed {
  width: 100%;
  height: 43px;
  position: fixed;
  z-index: 999;
}

.head {
  background: #3e63a8;
  color: #fff;
  width: 100%;
  box-sizing: border-box;
  padding: 10px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 12px;
}

.back {
  width: 14px;
  height: 14px;
}

.head div img {
  width: 20px;
  height: 20px;
}

.head-search {
  box-sizing: border-box;
  flex: 1;
  text-align: center;
  font-size: 14px;
}

.content {
  position: relative;
  top: 43px;
  width: 100%;
  box-sizing: border-box;
  padding: 0 20px;
}

.inpBox {
  width: 100%;
  box-sizing: border-box;
  display: flex;
  border-bottom: 1px solid #eee;
  justify-content: space-between;
  align-items: center;
  padding: 10px 0;
  font-size: 14px;
}

.inpBox img {
  width: 50px;
  height: 50px;
  border-radius: 50%;
}

.inpBox input {
  border: 0;
  flex: 1;
  outline: none;
  font-size: 14px;
  height: 30px;
  text-align: right;
}

.inpBox div {
  display: flex;
  align-items: center;
}

.inpBox div input[type="radio"] {
  margin-left: 20px;
  margin-right: 5px;
}

.inpBoxs {
  width: 100%;
  box-sizing: border-box;
  display: flex;
  flex-flow: column;
  justify-content: space-between;
  align-items: center;
  font-size: 14px;
}

.inpBoxs .inpBox {
  border: 0;
}

.inpBoxs textarea {
  width: 100%;
  height: 150px;
  margin-top: 10px;
  box-sizing: border-box;
  padding: 10px;
}
</style>
